import {useState,useEffect} from 'react';
import qs from 'qs';
/**
 * 自定义hooks以use开头,里面会调用其它的React Hooks
 * @param {*} url 
 * @returns 
 */
function useRequest(url){
    //设置一个分页的对象 当前是第几页 current,每页多少条 pageSize
    let [pagination,setPagination] = useState({current:1,pageSize:10});
    let [data,setData] = useState({list:[],total:0});
    //http://localhost:8000/api/users?current=1&pageSize=10
    useEffect(()=>{
        //调用远程接口,获取接口的响应体,然后设置到data状态中
        fetch(url+'?'+qs.stringify(pagination)).
            then(res=>res.json()).
            then(data=>setData(data));
    },[pagination]);
    return [data,pagination,setPagination];
}
export default useRequest;